type Status = 'pending' | 'fullfiled'; // 状态  进行中 // 状态  成功
type TaskFunc = (resId: string) => Promise<string>;
export class SketchLineCreateTask {
  private task: TaskFunc[];
  private resultId: string;
  public runCount: number;
  public status: Status = 'fullfiled';
  static instance: SketchLineCreateTask;
  constructor() {
    this.task = [];
    this.runCount = 0;
  }
  clear() {
    this.resultId = null;
  }
  add(task: TaskFunc) {
    this.task.push(task);
    if (this.status === 'fullfiled') {
      this._run();
    }
    this.status = 'pending';
  }
  _run() {
    while (this.task.length > 0 && this.runCount < 1) {
      this.runCount++;
      const task = this.task.shift();
      task(this.resultId)
        .then((res: string) => {
          this.resultId = res;
          this.runCount--;
          this._run();
          if (this.task.length === 0) {
            this.status = 'fullfiled';
          }
        })
        .finally(() => {});
    }
  }
}
